//
//  UITableViewCell+HDCityCell.m
//  app
//
//  Created by D on 15/5/12.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HDCityCell.h"
#import "HDProvinceModel.h"
#import "HDCityModel.h"
#import "hdcityButton.h"


@interface HDCityCell ()

@property (nonatomic,strong)NSMutableArray * subArray;

@end

@implementation HDCityCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifie
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifie]) {
        self.backgroundColor = [UIColor clearColor];
         self.subArray = [[NSMutableArray alloc]init];
        
    }
    return self;
}

-(void)cellWithModel:(NSArray *)model Section:(NSString*)sectionSign
{
    CGFloat threeEachWith = SCREEN_WIDTH/3;
    CGFloat threeEachHight = threeEachWith/2;
    int count = (int)model.count;
    [self.subArray removeAllObjects];
    
    if ([sectionSign isEqualToString:@"热门城市"]) {
        sectionSign = @"1";
        for (int i = 0 ; i < count; i++) {
            HDCityModel * cityModel = model[i];
            
            
            if ( cityModel.hot.integerValue == 1) {
                [self.subArray addObject:cityModel];
                
            }else if ([cityModel.Pyinsort isEqualToString:sectionSign]){
                
                [self.subArray addObject:cityModel];
            }
        }

    }else
    {
        for (int i = 0 ; i < count; i++) {
            HDCityModel * cityModel = model[i];
            
            
            if ([cityModel.Pyinsort isEqualToString:sectionSign]){
                
                [self.subArray addObject:cityModel];
            }
        }
    }
    
        for (int j = 0 ; j < self.subArray.count ; j ++) {
        
        HDCityButton * cityBtn = [[HDCityButton alloc]initWithFrame:CGRectMake(j%3*threeEachWith, j/3*threeEachHight, threeEachWith+UP(1), threeEachHight+UP(1))];
        HDCityModel * cityModel = self.subArray[j];
        [cityBtn setTitle:[NSString stringWithFormat:@"%@",cityModel.cityName] forState:UIControlStateNormal];
        cityBtn.provinceName = cityModel.provinceName;
        cityBtn.tag = cityModel.pid.integerValue;
        cityBtn.titleLabel.font = JKAppGlobleFontMenu;
        [cityBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [cityBtn addTarget:self action:@selector(citySub:) forControlEvents:UIControlEventTouchUpInside];

        
        [self addSubview:cityBtn];
        
        
    }
    _cellHeight = [self heightOfCellWithSubBtnCount:self.subArray.count];
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, _cellHeight);

        [self.delegate cellHight:_cellHeight];
}
-(void)drawRect:(CGRect)rect
{
    CGFloat threeEachWith = SCREEN_WIDTH/3;
    CGFloat threeEachHight = threeEachWith/2;
    
    if (self.subArray.count%3 == 0) {
        for (int i = 0 ; i < self.subArray.count/3-1; i++) {
            [self drawHorizonLineLeft:UP(0) right:UP(0) at:(i+1)*threeEachHight];
            
        }
        for (int j = 0 ; j < 2; j++) {
            [self drawSingleLineFromPoint:CGPointMake(threeEachWith*(j+1), 0) toPoint:CGPointMake(threeEachWith*(j+1), _cellHeight) color:nil];
            
        }
    }else if (self.subArray.count%3 == 1)
    {
        for (int i = 0 ; i < self.subArray.count/3; i++) {
            [self drawHorizonLineLeft:UP(0) right:UP(0) at:(i+1)*threeEachHight];
        }
         [self drawSingleLineFromPoint:CGPointMake(threeEachWith, 0) toPoint:CGPointMake(threeEachWith, _cellHeight) color:nil];
         [self drawSingleLineFromPoint:CGPointMake(threeEachWith*2, 0) toPoint:CGPointMake(threeEachWith*2, _cellHeight-threeEachHight) color:nil];
    }else if (self.subArray.count%3 == 2)
    {
        for (int i = 0 ; i < self.subArray.count/3; i++) {
            [self drawHorizonLineLeft:UP(0) right:UP(0) at:(i+1)*threeEachHight];
        }
        for (int j = 0 ; j < 2; j++) {
            [self drawSingleLineFromPoint:CGPointMake(threeEachWith*(j+1), 0) toPoint:CGPointMake(threeEachWith*(j+1), _cellHeight) color:nil];
            
        }
    }
    
}


-(CGFloat)heightOfCellWithSubBtnCount:(NSInteger)count
{
    if (count%3 == 0) {
        return SCREEN_WIDTH/3/2*(count/3);
    }else
        return SCREEN_WIDTH/3/2*(count/3+1);
}

-(void)citySub:(HDCityButton*)sender
{
    NSLog(@"1111%@",sender.titleLabel.text);
    if (self.delegate && [self.delegate respondsToSelector:@selector(cellSelectClickHeaderButtonWithButton:)]) {
        
        [self.delegate cellSelectClickHeaderButtonWithButton:sender];
        
    }
}

@end
